import javax.inject.Inject;
import java.math.BigDecimal;

public class WithdrawCommand extends BigDecimalCommand {
    private final Database.Account account;
    private final Outputter outputter;
    private final BigDecimal minimumBalance;
    private final BigDecimal maximumWithdrawal;

    @Inject
    WithdrawCommand(Outputter outputter,
                    Database.Account account,
                    @MinimumBalance BigDecimal minimumBalance,
                    @MaximumWithdrawal BigDecimal maximumWithdrawal
                    ) {
        super(outputter);
        this.account = account;
        this.outputter = outputter;
        this.minimumBalance = minimumBalance;
        this.maximumWithdrawal = maximumWithdrawal;
    }

    @Override
    protected void handleAmount(BigDecimal amount) {
        if (amount.compareTo(maximumWithdrawal) > 0) {
            outputter.output("withdraw amount: " + amount + " large than limit: " + maximumWithdrawal);
            return;
        }
        BigDecimal newBalance = account.balance().subtract(amount);
        if (newBalance.compareTo(minimumBalance) < 0) {
            outputter.output("withdraw amount: " + amount + " large than your balance: " + account.balance());
        } else {
            account.withdraw(amount);
            outputter.output("your new balance is: " + account.balance());
        }

    }
}
